//
//  HJStatusCell.swift
//  HJWeiBo
//
//  Created by 黄珏 on 15/11/1.
//  Copyright © 2015年 黄珏. All rights reserved.
//

import UIKit

class HJStatusCell: UITableViewCell {
    /// 配图宽度约束
    var pictureViewWidthCon: NSLayoutConstraint?
    
    /// 配图高度约束
    var pictureViewHeightCon: NSLayoutConstraint?
    // MARK: - 属性
    /// 微博模型
    var status: HJStatus? {
        didSet {
            // 将模型赋值给 topView
            topView.status = status
            
            // 设置微博内容
            contentLabel.text = status?.text
            
//            配图
            pictureView.status = status
            let size = pictureView.calViewSize()
            
            pictureViewWidthCon?.constant = size.width
            pictureViewHeightCon?.constant = size.height
        }
    }
    
    func rowHeight(status: HJStatus) -> CGFloat {
        // 设置cell的模型
        self.status = status
        
        // 更新约束
        layoutIfNeeded()
        
        // 获取子控件的最大Y值
        let maxY = CGRectGetMaxY(bottomView.frame)
        return maxY
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    func prepareUI() {
//        添加子空间
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        topView.translatesAutoresizingMaskIntoConstraints = false
        contentLabel.translatesAutoresizingMaskIntoConstraints = false
        pictureView.translatesAutoresizingMaskIntoConstraints = false
        bottomView.translatesAutoresizingMaskIntoConstraints = false
//        给topView添加约束
        contentView.addConstraint(NSLayoutConstraint(item: topView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 8))
        contentView.addConstraint(NSLayoutConstraint(item: topView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: topView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width))
        contentView.addConstraint(NSLayoutConstraint(item: topView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 44))
//        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        
//        给contentLabel添加约束
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: topView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 8))
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: topView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 16))
//        给pictureView添加约束
//        let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 0, height: 0), offset: CGPoint(x: 0, y: 8))
//        
//        // 获取配图的宽高约束
//        pictureViewHeightCon = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
//        pictureViewWidthCon = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//        给bottom添加约束
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -16, y: 8))
    }
    

//    MARK: - 懒加载
    lazy var topView: HJTopView = HJTopView()
    lazy var contentLabel: UILabel = {
        let label = UILabel(fonsize: 16, textColor: UIColor.darkTextColor())
        label.numberOfLines = 0
        return label
    }()
    
    lazy var pictureView: HJPictureView = HJPictureView()
    lazy var bottomView: HJBottomView = HJBottomView()
}
